package com.me.modules.sys.controller;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.utils.PageUtils;
import com.me.modules.common.utils.Query;
import com.me.modules.common.utils.Result;
import com.me.modules.sys.entity.OperationEntity;
import com.me.modules.sys.service.OperationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Date : 2018/12/18 16:05
 * @Author : me
 * @Desc : 业务系统
 */
@RestController
@RequestMapping("sys/operation")
public class OperationController extends BaseController {
    @Autowired
    private OperationService operationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:operation:all")
    @SysLog("查看业务系统列表")
    public Result list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<OperationEntity> operationList = operationService.queryList(query);
        int total = operationService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(operationList, total, query.getLimit(), query.getPage());

        return Result.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:operation:all")
    @SysLog("查看业务系统")
    public Result info(@PathVariable("id") Integer id){
        OperationEntity operation = operationService.queryObject(id);

        return Result.ok().put("operation", operation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:operation:all")
    @SysLog("保存业务系统")
    public Result save(@RequestBody OperationEntity operation){
        try {
            Integer id = operationService.save(operation);
            if(id == null){
                return Result.error("保存"+operation.getName()+"失败!");
            }
            return Result.ok("保存"+operation.getName()+"成功!").put("operationInfo",operation);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存"+operation.getName()+"失败!");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:operation:all")
    @SysLog("修改业务系统")
    public Result update(@RequestBody OperationEntity operationEntity){
        int count = operationService.update(operationEntity);
        if(count < 1){
            return Result.error("修改"+operationEntity.getName()+"失败!");
        }
        operationEntity = operationService.queryObject(operationEntity.getId());
        return Result.ok("修改"+operationEntity.getName()+"成功!").put("operationInfo",operationEntity);
    }


    /**
     * 查询业务系统机构树
     * @return
     */
    @RequestMapping(value = "/operationTree")
    @RequiresPermissions("sys:operation:all")
    public Result codeTree(){
        List<OperationEntity> operationEntities = operationService.queryListByBean(new OperationEntity());
        return Result.ok().put("operationTree", operationEntities);
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:operation:all")
    @SysLog("删除业务系统")
    public Result delete(@RequestBody String ids){
        operationService.updateIsdel(ids,1);

        return Result.ok("删除成功!");
    }
}
